package com.scuec.groupfive.mapper;

import com.github.pagehelper.Page;
import com.scuec.groupfive.entity.MockExam;
import com.scuec.groupfive.vo.MockExamVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface MockExamMapper {

    // 查询所有模拟考试记录（用于分页）
    @Select("SELECT " +
            "exam_id AS examId, " +
            "user_id AS userId, " +
            "username, " +
            "exam_year AS examYear, " +
            "exam_date AS examDate, " +
            "total_questions AS totalQuestions, " +
            "correct_questions AS correctQuestions, " +
            "total_time AS totalTime, " +
            "score, " +
            "created_at AS createdAt, " +
            "exam_type AS examType " +
            "FROM mock_exams ORDER BY exam_id DESC")
    List<MockExam> getAllMockExams();

    // 根据用户名查询模拟考试记录
    @Select("SELECT " +
            "m.exam_id AS examId, " +
            "m.user_id AS userId, " +
            "m.username, " +
            "m.exam_year AS examYear, " +
            "m.exam_date AS examDate, " +
            "m.total_questions AS totalQuestions, " +
            "m.correct_questions AS correctQuestions, " +
            "m.total_time AS totalTime, " +
            "m.score, " +
            "m.created_at AS createdAt, " +
            "m.exam_type AS examType, " +
            "ea.association_type AS associationType " +  // 添加别名
            "FROM mock_exams m " +
            "LEFT JOIN exam_association ea ON m.exam_id = ea.mock_exam_id " +
            "WHERE m.username = #{username} " +
            "ORDER BY m.exam_id DESC")
    List<MockExamVO> getMockExamsByUsername(@Param("username") String username);

    // 根据用户名查询模拟考试统计信息
    @Select("SELECT " +
            "COUNT(*) AS examCount, " +
            "AVG(score) AS averageScore " +
            "FROM mock_exams " +
            "WHERE username = #{username}")
    Map<String, Object> getExamStatsByUsername(@Param("username") String username);
}